package com.lijing.controller;

import com.lijing.model.Club;
import com.lijing.model.User;
import com.lijing.service.ClubService;
import com.lijing.service.UserService;
import com.lijing.util.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ClubController {
    @Autowired
    private ClubService clubService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addClub", produces = "application/json;charset=UTF-8")
    public String addClub(Club club){
        clubService.addClub(club);
        return JSONResponse.getOK("添加成功！");
    }

    @RequestMapping(value = "/updateClub", produces = "application/json;charset=UTF-8")
    public String updateClub(Club club){
        clubService.updateClub(club);
        return JSONResponse.getOK("修改成功！");
    }

    @RequestMapping(value = "/deleteClub", produces = "application/json;charset=UTF-8")
    public String deleteClub(Integer cid){
        clubService.deleteClub(cid);
        return JSONResponse.getOK("已删除");
    }

    // 获取社团信息
    @RequestMapping(value = "/getClub")
    public Club getClub(Integer cid){
        Club club = clubService.getClub(cid);
        club.setMember_count(clubService.getMemberCount(club));
        return club;
    }

    @RequestMapping(value = "/getClubList")
    public List<Club> getClubList(){
        List<Club> clubList = clubService.getClubList();
        for (Club club:clubList){
            club.setMember_count(clubService.getMemberCount(club));
        }
        return clubList;
    }

    // 获取社团成员
    @RequestMapping(value = "/clubMember")
    public List<User> clubMember(Integer cid){
        return clubService.getMember(cid);
    }

    // 删除社团成员
    @RequestMapping(value = "/deleteMember")
    public String deleteMember(@RequestParam("uids") Integer[] uids){
        User user = null;
        for (int uid:uids){
            System.out.println(uid);
            user = userService.getUser(uid);
            user.setClub(null);
            userService.removeMember(user);
        }
        return JSONResponse.getOK("删除成功");
    }

    // 批量删除社团成员
    @RequestMapping(value = "/deleteMembers")
    public String deleteMember(@RequestParam("users") User[] users){
        for(User user:users){
            userService.removeMember(user);
        }
        return JSONResponse.getOK("删除成功");
    }

    // 添加社团成员
    @RequestMapping(value = "/addMember", produces = "application/json;charset=UTF-8")
    public String addMember(String username, Integer cid){
        User user = userService.getUserByUsername(username);
        if (user == null){
            return JSONResponse.getError(-1,"没有此用户");
        }
        if (user.getClub() != null && user.getClub().getCid() == cid){
            return JSONResponse.getError(-2,"用户已加入该社团");
        }
        if (user.getClub() != null){
            return JSONResponse.getError(-3, "用户已加入其它社团");
        }
        user.setClub(clubService.getClub(cid));
        userService.update(user);
        return JSONResponse.getOK("添加成功");
    }

    // 任命社长
    @RequestMapping(value = "/setManager", produces = "application/json;charset=UTF-8")
    public String setManager(Integer cid, String no){
        Club club = clubService.getClub(cid);
        User user = userService.getUserByUsername(no);
        if (user == null){
            return JSONResponse.getError(-1, "不存在此用户");
        }else {
            club.setManager(user);
            clubService.updateClub(club);
            return JSONResponse.getOK("已设置" + user.getUname() + "为" + club.getCname() + "社长");
        }
    }
}
